#    SUSAN® - Sole of Unix Save ANything
#
#   Copyright (C) 2017-2020 Hechzh Property
#
#   This program is Free Software; you can redistribute it and/or
#   modify it under the terms of version three of the GNU Affero General Public
#   License as published by the Free Software Foundation and included
#   in the file LICENSE.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301, USA.
message("Entering ${CMAKE_CURRENT_SOURCE_DIR}")

if(NOT client-only)
  set(INCLUDE_FILES
      ../include/baconfig.h
      ../include/susan.h
      ../include/bc_types.h
      ../include/config.h
      ../include/jcr.h
      version.h
      address_conf.h
      alist.h
      attr.h
      base64.h
      berrno.h
      bits.h
      bpipe.h
      breg.h
      bregex.h
      bstringlist.h
      bsock.h
      bsock_tcp.h
      btime.h
      btimers.h
      cbuf.h
      crypto.h
      crypto_cache.h
      devlock.h
      dlist.h
      fnmatch.h
      guid_to_name.h
      htable.h
      ini.h
      lex.h
      lib.h
      lockmgr.h
      md5.h
      mem_pool.h
      message.h
      messages_resource.h
      messages_resource_items.h
      mntent_cache.h
      parse_conf.h
      plugins.h
      qualified_resource_name_type_converter.h
      rblist.h
      runscript.h
      rwlock.h
      scsi_crypto.h
      scsi_lli.h
      scsi_tapealert.h
      serial.h
      sha1.h
      status_packet.h
      thread_list.h
      tls.h
      tls_conf.h
      tree.h
      try_tls_handshake_as_a_server.h
      var.h
      watchdog.h
  )

  install(FILES ${INCLUDE_FILES} DESTINATION ${includedir})
endif()

include_directories(
  ${OPENSSL_INCLUDE_DIR} ${PTHREAD_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS}
  ${ACL_INCLUDE_DIRS} ${LZO2_INCLUDE_DIRS} ${CAP_INCLUDE_DIRS}
  ${WRAP_INCLUDE_DIRS}
)

set(SUSAN_SRCS
    address_conf.cc
    alist.cc
    attr.cc
    attribs.cc
    backtrace.cc
    base64.cc
    berrno.cc
    bget_msg.cc
    binflate.cc
    bnet_server_tcp.cc
    bnet.cc
    bnet_network_dump.cc
    bnet_network_dump_private.cc
    bpipe.cc
    breg.cc
    bregex.cc
    bsnprintf.cc
    bsock.cc
    bsock_tcp.cc
    bstringlist.cc
    bsys.cc
    btime.cc
    btimers.cc
    cbuf.cc
    connection_pool.cc
    cram_md5.cc
    crypto.cc
    crypto_cache.cc
    crypto_none.cc
    crypto_nss.cc
    crypto_openssl.cc
    crypto_wrap.cc
    daemon.cc
    devlock.cc
    dlist.cc
    edit.cc
    fnmatch.cc
    guid_to_name.cc
    hmac.cc
    htable.cc
    jcr.cc
    json.cc
    lockmgr.cc
    md5.cc
    mem_pool.cc
    message.cc
    messages_resource.cc
    mntent_cache.cc
    output_formatter.cc
    output_formatter_resource.cc
    passphrase.cc
    path_list.cc
    plugins.cc
    bpoll.cc
    priv.cc
    recent_job_results_list.cc
    rblist.cc
    runscript.cc
    rwlock.cc
    scan.cc
    scsi_crypto.cc
    scsi_lli.cc
    serial.cc
    sha1.cc
    signal.cc
    status_packet.cc
    thread_list.cc
    thread_specific_data.cc
    timer_thread.cc
    tls.cc
    tls_conf.cc
    tls_openssl.cc
    tls_openssl_crl.cc
    tls_openssl_private.cc
    tree.cc
    try_tls_handshake_as_a_server.cc
    compression.cc
    util.cc
    var.cc
    watchdog.cc
    watchdog_timer.cc
)

if(HAVE_WIN32)
  list(APPEND SUSAN_SRCS ../win32/compat/compat.cc ../win32/compat/glob.cc
       ../win32/compat/winapi.cc
  )
else()
  list(APPEND SUSAN_SRCS scsi_tapealert.cc)
endif()

set(SUSANCFG_SRCS
    susan_resource.cc
    configured_tls_policy_getter.cc
    ini.cc
    lex.cc
    parse_bsr.cc
    res.cc
    parse_conf.cc
    parse_conf_init_resource.cc
    parse_conf_state_machine.cc
    res.cc
    qualified_resource_name_type_converter.cc
)
list(APPEND SUSAN_SRCS ${SUSANCFG_SRCS})

if(HAVE_WIN32)
  list(APPEND SUSAN_SRCS osinfo_win32.cc)
else()
  list(APPEND SUSAN_SRCS osinfo.cc)
endif()

add_library(susan SHARED ${SUSAN_SRCS} $<TARGET_OBJECTS:version-obj>)

add_library(version-obj OBJECT version.cc)
target_compile_definitions(
  version-obj
  PRIVATE -DSUSAN_VERSION="${SUSAN_FULL_VERSION}"
          -DSUSAN_DATE="${DATE}"
          -DSUSAN_SHORT_DATE="${SUSAN_SHORT_DATE}"
          -DSUSAN_PROG_DATE_TIME="${SUSAN_PROG_DATE_TIME}"
          -DSUSAN_YEAR="${SUSAN_YEAR}"
)
set_target_properties(version-obj PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_link_libraries(
  susan
  susanfastlz
  ${OPENSSL_LIBRARIES}
  ${PTHREAD_LIBRARIES}
  ${ZLIB_LIBRARIES}
  ${ACL_LIBRARIES}
  ${LZO2_LIBRARIES}
  ${CAP_LIBRARIES}
  ${WRAP_LIBRARIES}
  ${CAM_LIBRARIES}
  ${WINDOWS_LIBRARIES}
  ${JANSSON_LIBRARIES}
)

install(TARGETS susan DESTINATION ${libdir})

set_target_properties(
  susan PROPERTIES VERSION "${SUSAN_NUMERIC_VERSION}"
                    SOVERSION "${SUSAN_VERSION_MAJOR}"
)

if(HAVE_WIN32)
  set_target_properties(susan PROPERTIES DEFINE_SYMBOL "BUILDING_DLL")
endif()
